package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguMusicRetentConfigDao;
import org.jsbd.boss.domian.MiguMusicRetentConfigBean;
import org.jsbd.boss.service.IMiguMusicRetentConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.utils.StringUtils;

@Service("miguMusicRetentConfigService")
public class MiguMusicRetentConfigServiceImpl implements IMiguMusicRetentConfigService {

	@Autowired
	private IMiguMusicRetentConfigDao miguMusicRetentConfigDao;
	@Autowired
	private RedisClient<String,Object> redisClientMigu;

	@Override
	public void saveMiguMusicRetentConfig(MiguMusicRetentConfigBean miguMusicRetentConfig){
		miguMusicRetentConfigDao.saveMiguMusicRetentConfig(miguMusicRetentConfig);
	}

	@Override
	public void updateMiguMusicRetentConfig(MiguMusicRetentConfigBean miguMusicRetentConfig){
		miguMusicRetentConfigDao.updateMiguMusicRetentConfig(miguMusicRetentConfig);
	}

	@Override
	public MiguMusicRetentConfigBean getMiguMusicRetentConfig(MiguMusicRetentConfigBean miguMusicRetentConfig) {
		return miguMusicRetentConfigDao.getMiguMusicRetentConfig(miguMusicRetentConfig);
	}

	@Override
	public List<MiguMusicRetentConfigBean> getMiguMusicRetentConfigList(MiguMusicRetentConfigBean miguMusicRetentConfig) {
		List<MiguMusicRetentConfigBean> configList = miguMusicRetentConfigDao.getMiguMusicRetentConfigList(miguMusicRetentConfig);
		if(configList==null||configList.isEmpty()){
			return new ArrayList<MiguMusicRetentConfigBean>();
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_RENTENT_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_RETENT_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUMUSIC_RENTENT_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUMUSIC_RETENT_TASK_NUM_DONE;
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguMusicRetentConfigBean config:configList){
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			
			if(config.getRetentRate()==null){
				totalNum=0;
			}else{
				totalNum=totalNum*config.getRetentRate()/100;
			}
			
			String key=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay+","+config.getRetentDayStr();
			String successNum=(String)successMap.get(key);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			String doKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay+"_"+config.getRetentDayStr();
			Long doNum=(Long)doNumMap.get(doKey);
			if(doNum==null){
				doNum=0L;
			}
			
            int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=totalNum+"|"+planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		return configList;
	}

	@Override
	public Map<String, Object> getMiguMusicRetentConfigListPage(DataGridModel page, MiguMusicRetentConfigBean miguMusicRetentConfig) {
		Map<String, Object> results=miguMusicRetentConfigDao.getMiguMusicRetentConfigListPage(page, miguMusicRetentConfig);
		List<MiguMusicRetentConfigBean> configList=(List<MiguMusicRetentConfigBean>)results.get("rows");
		if(configList==null||configList.isEmpty()){
			return results;
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_RENTENT_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_RETENT_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUMUSIC_RENTENT_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUMUSIC_RETENT_TASK_NUM_DONE;
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguMusicRetentConfigBean config:configList){
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			
			if(config.getRetentRate()==null){
				totalNum=0;
			}else{
				totalNum=totalNum*config.getRetentRate()/100;
			}
			
			String key=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay+","+config.getRetentDayStr();
			String successNum=(String)successMap.get(key);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			String doKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay+"_"+config.getRetentDayStr();
			Long doNum=(Long)doNumMap.get(doKey);
			if(doNum==null){
				doNum=0L;
			}
			
            int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=totalNum+"|"+planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		return results;
	}

	@Override
	public Integer getMiguMusicRetentConfigCount(MiguMusicRetentConfigBean miguMusicRetentConfig) {
		return miguMusicRetentConfigDao.getMiguMusicRetentConfigCount(miguMusicRetentConfig);
	}
	
	@Override
	public void deleteMiguMusicRetentConfig(MiguMusicRetentConfigBean miguMusicRetentConfig) {
		miguMusicRetentConfigDao.deleteMiguMusicRetentConfig(miguMusicRetentConfig);
	}

	@Override
	public List<MiguMusicRetentConfigBean> getByMiguMusicId(Integer id) {
		return miguMusicRetentConfigDao.selectByMiguMusicId(id);
	}

	@Override
	public void copyMiguMusicRetentConfig(List<MiguMusicRetentConfigBean> targetList) {
		miguMusicRetentConfigDao.insertMiguMusicRetentConfig(targetList);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public void saveMiguMusicRetentConfigNew(List<MiguMusicRetentConfigBean> list) {
		miguMusicRetentConfigDao.insertMiguMusicRetentConfig(list);
	}

	@Override
	public void deleteByMiguMusicId(Integer id) {
		miguMusicRetentConfigDao.deleteByMiguMusicId(id);
	}

	@Override
	public void saveRetent(List<MiguMusicRetentConfigBean> mvrcs) {
		miguMusicRetentConfigDao.saveRetent(mvrcs);
	}

	@Override
	public void deleteRetentBatch(MiguMusicRetentConfigBean mvrcb) {
		miguMusicRetentConfigDao.deleteRetentBatch(mvrcb);
	}
	
}
